package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Order;import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/*
 * session资源过滤器
 * */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;

    /**
     * 用户注册方法
     */
    @PostMapping
    public JsonResult register(User user) {
        JsonResult result = new JsonResult();
        try{
            userService.register(user);
        }catch (Exception e){
            e.getStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }
    @PutMapping("/{id}")
    public JsonResult update(User user) {
        JsonResult result = new JsonResult();
        userService.updateByUserId(user);
        result.setResult(user);
        return result;
    }
    @GetMapping("/{authorId}/travels")
    public PageInfo queryTravel(TravelQueryObject qo){
        qo.setOrderBy("t.lastUpdateTime DESC");
        PageInfo<Travel> pageInfo = travelService.query(qo);
        return pageInfo;
    }
    @GetMapping
    public PageInfo query(UserQueryObject qo){
        PageInfo<User> pageInfo = userService.query(qo);
        return pageInfo;
    }

    /**
     * 根据email查询头像
     * @param
     * @return
     */
    @GetMapping("/email")
    public User selectCoverUrl(User user){
        return userService.selectCoverUrl(user);
    }

    /**
     * 点击关注 保存数据
     * @param userId  当前登录用户Id
     * @param hearImgUrlId    被点击的头像的Id
     * @return
     */
    @PostMapping("/save")
    public JsonResult saveAandF(Long userId,Long hearImgUrlId) {
        System.out.println(userId);
        System.out.println(hearImgUrlId);
        JsonResult result = new JsonResult();
        userService.saveAandF(userId,hearImgUrlId);
        return result;
    }

    /**
     * 点击取消关注
     * @param userId
     * @param hearImgUrlId
     * @return
     */
    @PutMapping("/update")
    public JsonResult update(Long userId,Long hearImgUrlId) {
        System.out.println(userId);
        System.out.println(hearImgUrlId);
        JsonResult result = new JsonResult();
        userService.updateAandF(userId,hearImgUrlId);
        return result;
    }

    /**
     * 查询头像Id的数据
     * @param id
     * @return
     */
    @GetMapping("/id")
    public User selectById(Long id){
        User user = userService.selectById(id);
        return user;
    }

    /**
     * 查询粉丝和关注的数量
     * @param id
     * @return
     */
    @GetMapping("/selectAandF")
    public Map selectAandF(Long id){
        return userService.selectAandF(id);

    }

    @GetMapping("/selectStateByUserId")
    public Map selectStateByUserId(Long userId,Long hearImgUrlId){
        return userService.selectStateByUserId(userId,hearImgUrlId);

    }

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("/tickets")
    public JsonResult saveUserAndTicket(Order order) {
        System.out.println(order.getUser().getId());
        JsonResult result = new JsonResult();
        userService.saveUserAndTicket(order);
        return result;
    }

    /**
     * 查询我的订单
     * @param qo
     * @return
     */
    @GetMapping("/orders")
    public PageInfo selectOrderByUserId(UserQueryObject qo){
        //不分页
        qo.setPageSize(0);
        qo.setOrderBy("ut.createTime DESC");
        return userService.selectOrderByUserId(qo);

    }
}
